package com.zzyl.service.impl.handler;

import com.zzyl.entity.Resource;
import com.zzyl.vo.TreeItemVo;

import java.util.List;
import java.util.stream.Collectors;

public class ThirdFilterHandler implements ResourceFilterHandler{
    private ResourceFilterHandler nextHanlder;
    public void setNextHanlder(ResourceFilterHandler nextHanlder) {
        this.nextHanlder = nextHanlder;
    }
    @Override
    public void filter(List<Resource> listResource, TreeItemVo root) {
    //6. 遍历每一个二级目录 然后找找对应的二级目录下的三级目录 并构建关系
        root.getChildren().stream().map(first -> {
            first.getChildren().stream().map(second -> {
                List<Resource> thirdTree = listResource.stream().filter(r -> {
                    if (r.getParentResourceNo().equals(second.getId())) {
                        return true;
                    } else {
                        return false;
                    }
                }).collect(Collectors.toList());

                List<TreeItemVo> thirdTreeList = thirdTree.stream().map(r -> {
                    TreeItemVo vo = new TreeItemVo();
                    vo.setId(r.getResourceNo());
                    vo.setLabel(r.getResourceName());
                    return vo;
                }).collect(Collectors.toList());

                second.setChildren(thirdTreeList);

                return second;
            }).collect(Collectors.toList());

            return first;
        }).collect(Collectors.toList());

        if(null != nextHanlder){
            nextHanlder.filter(listResource, root);
        }
    }
}
